﻿using System;
using System.Collections.Generic;
using System.Data.Objects;
using System.Linq;

namespace Project5188.BUS.Operations
{
    public class DressInfos : DetailView<dressinfo>
    {
        private project5188Entities _context;
        // ReSharper disable InconsistentNaming
        private new List<dressinfo> EntityList;
        private new dressinfo Entity;
        // ReSharper restore InconsistentNaming

        public long TransactionId { get; private set; }

        #region Overrides of DetailView<dressinfo>

        public override List<dressinfo> GetEntityList()
        {
            _context = new project5188Entities();
            EntityList = _context.dressinfoes.Where(a => !a.IsDeleted).OrderBy(a => a.a_ID).ToList();

            return EntityList;
        }

        public override List<dressinfo> GetEntityById(long id)
        {
            _context = new project5188Entities();
            EntityList = _context.dressinfoes.Where(a => a.a_ID == id && !a.IsDeleted).OrderBy(a => a.a_ID).ToList();

            return EntityList;
        }

        public override dressinfo GetEntity()
        {
            _context = new project5188Entities();
            Entity = new dressinfo();
            return Entity;
        }

        public override void Update()
        {
            _context.SaveChanges();
        }

        public override void Insert()
        {
            if (ExistPersonnel(Entity.b_PersonnelID))
            {
                throw new Exception("Seçtiğiniz personel için sistemde kayıt mevcuttur.");
            }

            _context.AddTodressinfoes(Entity);
            _context.SaveChanges();
            TransactionId = Entity.a_ID;
        }

        #endregion

        public bool ExistPersonnel(long personnelId)
        {
            _context = new project5188Entities();

            return _context.dressinfoes.Where(a => a.b_PersonnelID == personnelId && !a.IsDeleted).Count() > 0;
        }

        public List<dressinfo> GetEntityListByParameters(string registryNoText, string nameSurname, string tcNoText)
        {
            _context = new project5188Entities();
            int tcNo = 0, registryNo = 0;

            if (!String.IsNullOrEmpty(tcNoText) && !String.IsNullOrWhiteSpace(tcNoText))
            {
                Int32.TryParse(tcNoText, out tcNo);
            }

            if (!String.IsNullOrEmpty(registryNoText) && !String.IsNullOrWhiteSpace(registryNoText))
            {
                Int32.TryParse(registryNoText, out registryNo);
            }

            var objectParameters = new ObjectParameter[3];

            var registryParameter = new ObjectParameter("RegistryNo", typeof(int)) { Value = registryNo };
            objectParameters[0] = registryParameter;

            var nameSurnameParameter = new ObjectParameter("NameSurname", typeof(string)) { Value = nameSurname };
            objectParameters[1] = nameSurnameParameter;

            var tcNoParameter = new ObjectParameter("TCNo", typeof(int)) { Value = tcNo };
            objectParameters[2] = tcNoParameter;

            var list = _context.ExecuteFunction<dressinfo>("GetDressInfoListByParameters", objectParameters).ToList();

            return list;
        }
    }
}
